<template>
  <div class="page-container">
    <!-- 顶部导航栏 -->
    <div class="header">
      <el-icon :size="20" class="back-icon" @click="goBack">
        <ArrowLeft />
      </el-icon>
      <h1>通知详情</h1>
      <div class="header-right"></div>
    </div>

    <!-- 详情内容 -->
    <div class="content">
      <div v-if="loading" class="loading-container">
        <el-icon class="is-loading" :size="32" color="#409eff">
          <Loading />
        </el-icon>
        <p>加载中...</p>
      </div>

      <div v-else-if="!notification" class="empty-container">
        <el-empty description="通知不存在" />
        <el-button type="primary" @click="goBack" style="margin-top: 20px">
          返回列表
        </el-button>
      </div>

      <div v-else class="detail-card">
        <!-- 通知标题 -->
        <div class="detail-header">
          <h2 class="detail-title">{{ notification.title }}</h2>
          <div class="detail-meta">
            <el-tag :type="getNotificationType(notification.type)" size="small">
              {{ getNotificationTypeText(notification.type) }}
            </el-tag>
            <span class="detail-time">{{ formatTime(notification.created_at) }}</span>
          </div>
        </div>

        <!-- 分割线 -->
        <el-divider />

        <!-- 通知内容 -->
        <div class="detail-content">
          <p v-html="formatContent(notification.content)"></p>
        </div>

        <!-- 附加信息（如果有） -->
        <div v-if="notification.extra_info" class="detail-extra">
          <el-divider content-position="left">
            <span class="divider-text">附加信息</span>
          </el-divider>
          <div class="extra-info-content">
            <div v-for="(value, key) in notification.extra_info" :key="key" class="info-item">
              <span class="info-label">{{ key }}：</span>
              <span class="info-value">{{ value }}</span>
            </div>
          </div>
        </div>

        <!-- 操作按钮 -->
        <div class="detail-actions">
          <el-button 
            v-if="notification.action_url" 
            type="primary" 
            @click="handleAction"
            :icon="Right"
          >
            {{ notification.action_text || '查看详情' }}
          </el-button>
          <el-button @click="handleDelete" :icon="Delete" type="danger" plain>
            删除通知
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ArrowLeft, Loading, Right, Delete } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const loading = ref(false)
const notification = ref(null)

onMounted(() => {
  loadNotificationDetail()
})

// 加载通知详情
const loadNotificationDetail = async () => {
  loading.value = true
  try {
    const id = route.params.id
    
    // TODO: 调用后端 API 获取通知详情
    // const response = await getNotificationDetail(id)
    // notification.value = response.data
    
    notification.value = null
    loading.value = false
  } catch (error) {
    loading.value = false
    console.error('加载通知详情失败:', error)
    ElMessage.error('加载失败')
  }
}

// 格式化时间
const formatTime = (timeStr) => {
  const time = new Date(timeStr)
  return time.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 格式化内容（换行转换为 <br>）
const formatContent = (content) => {
  if (!content) return ''
  return content.replace(/\n/g, '<br>')
}

// 获取通知类型标签类型
const getNotificationType = (type) => {
  const typeMap = {
    'system': 'info',
    'success': 'success',
    'warning': 'warning',
    'error': 'danger'
  }
  return typeMap[type] || 'info'
}

// 获取通知类型文本
const getNotificationTypeText = (type) => {
  const textMap = {
    'system': '系统通知',
    'success': '审核通知',
    'warning': '重要提醒',
    'error': '异常通知'
  }
  return textMap[type] || '通知'
}

// 处理操作按钮
const handleAction = () => {
  if (notification.value.action_url) {
    router.push(notification.value.action_url)
  }
}

// 删除通知
const handleDelete = async () => {
  try {
    await ElMessageBox.confirm(
      '确定要删除这条通知吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    // TODO: 调用 API 删除通知
    ElMessage.success('删除成功')
    router.back()
  } catch {
    // 用户取消操作
  }
}

// 返回上一页
const goBack = () => {
  router.back()
}
</script>

<style scoped>
.page-container {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.header {
  background: white;
  padding: 0 16px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  position: sticky;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
}

.back-icon {
  color: #606266;
  cursor: pointer;
  transition: color 0.3s;
  margin-right: 12px;
}

.back-icon:hover {
  color: #409eff;
}

.header h1 {
  flex: 1;
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.header-right {
  width: 20px;
}

.content {
  padding: 16px 16px 80px;
  min-height: calc(100vh - 56px);
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
  color: #909399;
}

.loading-container p {
  margin-top: 12px;
  font-size: 14px;
}

.empty-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 60px 0;
}

.detail-card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.detail-header {
  margin-bottom: 16px;
}

.detail-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 12px 0;
  line-height: 1.5;
}

.detail-meta {
  display: flex;
  align-items: center;
  gap: 12px;
}

.detail-time {
  font-size: 13px;
  color: #909399;
}

.detail-content {
  margin: 20px 0;
  font-size: 15px;
  color: #606266;
  line-height: 1.8;
}

.detail-content :deep(p) {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
}

.detail-extra {
  margin-top: 24px;
  padding-top: 20px;
  background-color: #f9fafb;
  border-radius: 6px;
  padding: 16px;
}

.divider-text {
  font-size: 14px;
  font-weight: 500;
  color: #606266;
}

.extra-info-content {
  margin-top: 12px;
}

.info-item {
  display: flex;
  padding: 8px 0;
  font-size: 14px;
  line-height: 1.6;
}

.info-label {
  color: #909399;
  min-width: 80px;
  flex-shrink: 0;
}

.info-value {
  color: #606266;
  flex: 1;
}

.detail-actions {
  margin-top: 32px;
  display: flex;
  gap: 12px;
  justify-content: center;
}

.detail-actions .el-button {
  min-width: 120px;
}

:deep(.el-divider--horizontal) {
  margin: 16px 0;
}
</style>

